#! /usr/bin/env python
# -*- coding: utf8 -*-

import sys

if sys.version_info[:2] == (2, 5):
    from xml.etree import cElementTree as et
else:
    import cElementTree as et
import zope.interface
import random

import base

if __name__ == '__main__':
    print "You shouldn't be running this file!"
    sys.exit()

class addParent(object):
    zope.interface.implements(base.IAlgorithm)
    name = "addParent"

    def __init__(self, node):
        self.__names  = []
        self.__single = False
        self.__path   = base.parser(node.find("./parameters/path").text.strip())

    def __call__(self, node, parentMap, root):
        darthVader  = parentMap[node]

        if node not in darthVader:
            return

        fname = self.__path[0]
        if type(fname) == list:
            fname = random.choice(fname)

        maisNovo = et.Element(fname)
        parentMap[node].remove(node)
        parentMap[node].append(maisNovo)

        parentMap[maisNovo] = parentMap[node]

        for i in self.__path[1:]:
            if type(i) == list:
                i = random.choice(i)

            if i == '-':
                continue

            newElement = et.Element(i)
            maisNovo.append(newElement)
            parentMap[newElement] = maisNovo
            maisNovo = newElement

        parentMap[node] = maisNovo
        maisNovo.append(node)

__plugins__ = (addParent, )
